package lily.phs.urh;

import lily.phs.object.HereTipa;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PerHealthFirstView extends RelativeLayout implements OnClickListener, OnFocusChangeListener{
	View v;
	LinearLayout ll1;
	//ScrollView sv;
	TextView tv;
	Paint paint;
	private int profileid;
	private Context myContext;
	private EditText et_id,et_name,et_nowplace,et_hjdz,et_tel,et_xzname,et_cwhname,
				     et_jddw,et_jdr,et_doctor,et_date;
	
	MyPopupWindow mPopupWindow;
	private HereTipa ht;
	
	public PerHealthFirstView(Context context,int id){
		super(context);
		this.myContext=context;
		this.profileid=id;
		System.out.println("new health="+profileid);
		paint=new Paint();
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(Color.BLACK);
		LayoutInflater factory = LayoutInflater.from(context);
		v=factory.inflate(R.layout.build_per_healthpro, null);
		ll1=(LinearLayout)v.findViewById(R.id.ll);
		et_id=(EditText)v.findViewById(R.id.et_jmjkda_id);
		//et_id.setInputType(InputType.TYPE_NULL);
		et_name=(EditText)v.findViewById(R.id.et_jmjkda_name);
//		et_name.setInputType(InputType.TYPE_NULL);
//		et_name.requestFocus();
		et_date=(EditText)v.findViewById(R.id.et_jmjkda_date);
		et_date.setInputType(InputType.TYPE_NULL);
		et_date.setOnClickListener(this);
		et_date.setOnFocusChangeListener(this);
		MyView mv=new MyView(myContext);
		LayoutParams lp=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);  
		this.addView(v,lp);
		this.addView(mv, lp);
		
		ht=new HereTipa(myContext);
		ht.selectHereTipa(profileid);
	}
	
	public void initWeidget(){
		//LayoutInflater factory = LayoutInflater.from(myContext);
		//v=factory.inflate(R.layout.build_per_healthpro, null);
		
		et_nowplace=(EditText)v.findViewById(R.id.et_jmjkda_nowplace);
		et_hjdz=(EditText)v.findViewById(R.id.et_jmjkda_hjdz);
		et_tel=(EditText)v.findViewById(R.id.et_jmjkda_tel);
		et_xzname=(EditText)v.findViewById(R.id.et_jmjkda_xzname);
		et_cwhname=(EditText)v.findViewById(R.id.et_jmjkda_cwhname);
		et_jddw=(EditText)v.findViewById(R.id.et_jmjkda_jddw);
		et_jdr=(EditText)v.findViewById(R.id.et_jmjkda_jdr);
		et_doctor=(EditText)v.findViewById(R.id.et_jmjkda_doctor);
		
		
	}
	
	public HereTipa selectData(){
		ht.selectHereTipa(profileid);
		return ht;
	}
	
	public void initView(){
		
		
		
		et_id.setText(ht.getHeti_here_id());
		et_name.setText(ht.getHeti_name());
		et_nowplace.setText(ht.getHeti_now_addr());
		et_hjdz.setText(ht.getHeti_hore_addr());
		et_tel.setText(ht.getHeti_tel());
		et_xzname.setText(ht.getHeti_town_name());
		et_cwhname.setText(ht.getHeti_vico_name());
		et_jddw.setText(ht.getHeti_buil_unit());
		et_jdr.setText(ht.getHeti_buil_per());
		et_doctor.setText(ht.getHeti_doctor());
		et_date.setText(ht.getHeti_buil_date());
	}
	
	public void saveView(){
		
		ht.setHeti_name(et_name.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_here_id(et_id.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_now_addr(et_nowplace.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_hore_addr(et_hjdz.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_tel(et_tel.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_town_name(et_xzname.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_vico_name(et_cwhname.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_buil_unit(et_jddw.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_buil_per(et_jdr.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_doctor(et_doctor.getText().toString().replaceAll("\\s", ""));
		ht.setHeti_buil_date(et_date.getText().toString().replaceAll("\\s", ""));
		ht.updateHereTipa(profileid);
	}
	
	
	class MyView extends View {
    	
    	Paint paint;
    	
		public MyView(Context context) {
			super(context);
			paint=new Paint();
			paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
		}
		
		@Override
		public void onDraw(Canvas c){
	        int top=ll1.getTop();
	        int left=ll1.getLeft();
	        int right=ll1.getRight();
	        int bottom=ll1.getBottom();
	        int y=0;//sv.getScrollY();
	        top=top-y;
	        bottom=bottom-y;
	        
	        c.drawLine(left, top, right, top, paint);
	        c.drawLine(left, top, left, bottom, paint);
	        c.drawLine(right, top, right, bottom, paint);
	        c.drawLine(left, bottom, right, bottom, paint);
		}
    	
    }
	
	public void setProfileid(int pro){
		this.profileid=pro;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_date){
			mPopupWindow = new MyPopupWindow(myContext);			
			mPopupWindow.showAtLocation(findViewById(R.id.et_jmjkda_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(v==et_date&&hasFocus){
			mPopupWindow = new MyPopupWindow(myContext);			
			mPopupWindow.showAtLocation(findViewById(R.id.et_jmjkda_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
	}
}
